﻿Imports System.Data

Partial Class WebForm_ManageEnlistment_AddEnlistment
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.FormView_EnlistHeader.InsertItemTemplate = Me.FormView_EnlistHeader.ItemTemplate
        Me.FormView_EnlistHeader.EditItemTemplate = Me.FormView_EnlistHeader.ItemTemplate

        Me.FormView_EnlistTrainingCourse.InsertItemTemplate = Me.FormView_EnlistTrainingCourse.EditItemTemplate
        'Me.FormView_EnlistTrainingCourse.EditItemTemplate = Me.FormView_EnlistTrainingCourse.ItemTemplate

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    '運用單位 
    Protected Sub ddlUOrganizationID_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim dt As DataTable
        Dim ddlUOrganizationID As DropDownList = TryCast(sender, DropDownList)

        If Me.DataAccessMode = FormViewMode.ReadOnly Then
            Dim sql As New MyStringBuilder
            Dim dat As New clsData

            sql.AppendFormat("select * from organizationview ")
            sql.AppendFormat("where UOrganizationID = '{0}' ", DataBinder.Eval(Me.FormView_EnlistHeader.DataItem, "UOrganizationID"))

            dt = dat.GetData(sql)
        Else
            dt = clsAuth.GetDDLDirectOrganizationData()

        End If

        If ddlUOrganizationID.Items.Count = 0 AndAlso (Not dt Is Nothing) AndAlso (dt.Rows.Count > 0) Then

            For Each objDataRow As DataRow In dt.Rows

                ddlUOrganizationID.Items.Add(New ListItem(objDataRow.Item("OrganizationName").ToString(), objDataRow.Item("UOrganizationID").ToString()))
            Next

            If ddlUOrganizationID.Items.Count > 1 Then
                ddlUOrganizationID.Items.Insert(0, New ListItem(Me.GetLocalResourceObject("PleaseSelect"), String.Empty))
            End If

        End If

    End Sub

    '課程類型
    Protected Sub ddlCourseType_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
        'TryCast(sender, DropDownList).Items.Insert(0, New ListItem(Me.GetLocalResourceObject("PleaseSelect"), String.Empty))
    End Sub

    Protected Sub FormView_EnlistTrainingCourse_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView_EnlistTrainingCourse.DataBound
        If Me.DataAccessMode = FormViewMode.ReadOnly Then
            Me.FormView_EnlistTrainingCourse.FindControl("lbtnAddCourse").Visible = False
        Else
            Me.FormView_EnlistTrainingCourse.FindControl("lbtnAddCourse").Visible = True
        End If
    End Sub

    '課程新增-FormView_EnlistTrainingCourse_ItemInserting
    Protected Sub FormView_EnlistTrainingCourse_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles FormView_EnlistTrainingCourse.ItemInserting

        e.Values.Item("StartDate") = TryCast(FormView_EnlistTrainingCourse.FindControl("txtCourseStartDate"), WebForm_CommonControl_CalendarTextBox).Text
        e.Values.Item("EndDate") = TryCast(FormView_EnlistTrainingCourse.FindControl("txtCourseEndDate"), WebForm_CommonControl_CalendarTextBox).Text
        e.Values.Item("EnlistID") = EnlistID
        e.Values.Item("CreateBy") = Page.User.Identity.Name
    End Sub

    '課程更新-GridView_EnlistTrainingCourse_RowUpdating
    Protected Sub GridView_EnlistTrainingCourse_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView_EnlistTrainingCourse.RowUpdating
        e.NewValues.Item("StartDate") = TryCast(GridView_EnlistTrainingCourse.Rows.Item(e.RowIndex).FindControl("txtCourseStartDate_Edit"), WebForm_CommonControl_CalendarTextBox).Text
        e.NewValues.Item("EndDate") = TryCast(GridView_EnlistTrainingCourse.Rows.Item(e.RowIndex).FindControl("txtCourseEndDate_Edit"), WebForm_CommonControl_CalendarTextBox).Text
        e.NewValues.Item("UpdateBy") = Page.User.Identity.Name
    End Sub

    Protected Sub ObjectDataSource2_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs) Handles ObjectDataSource2.Inserted
        GridView_EnlistTrainingCourse.DataBind() '因為一個DataSource備2個控件使用
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If Page.IsValid = False Then
            Return
        End If

        If DataAccessMode = FormViewMode.Insert Then
            FormView_EnlistHeader.InsertItem(True)

        ElseIf DataAccessMode = FormViewMode.Edit Then
            FormView_EnlistHeader.UpdateItem(True)

        End If

        RaiseEvent Save_Click(Me, New EventArgs)

    End Sub

    Public Event Check_Click(ByVal source As Object, ByVal e As EventArgs)

    Protected Sub lbtnCheck_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        CourseID = GridView_EnlistTrainingCourse.DataKeys(TryCast(TryCast(sender, LinkButton).NamingContainer, GridViewRow).DataItemIndex).Value
        RaiseEvent Check_Click(Me, New EventArgs)
    End Sub

    Protected Sub FormView_EnlistHeader_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles FormView_EnlistHeader.ItemCommand
        If e.CommandName = "btnSelectOwner" Then
            Me.SearchInternalUserControl1.Show()
        End If
    End Sub

    Protected Sub FormView_EnlistHeader_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles FormView_EnlistHeader.ItemInserting
        e.Values.Item("ApplyStartDate") = TryCast(FormView_EnlistHeader.FindControl("txtApplyStartDate"), WebForm_CommonControl_CalendarTextBox).Text
        e.Values.Item("ApplyEndDate") = TryCast(FormView_EnlistHeader.FindControl("txtApplyEndDate"), WebForm_CommonControl_CalendarTextBox).Text
        e.Values.Item("StartDate") = TryCast(FormView_EnlistHeader.FindControl("txtStartDate"), WebForm_CommonControl_CalendarTextBox).Text
        e.Values.Item("EndDate") = TryCast(FormView_EnlistHeader.FindControl("txtEndDate"), WebForm_CommonControl_CalendarTextBox).Text
        e.Values.Item("CreateBy") = Page.User.Identity.Name
    End Sub

    Protected Sub FormView_EnlistHeader_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdateEventArgs) Handles FormView_EnlistHeader.ItemUpdating
        e.NewValues("ApplyStartDate") = TryCast(FormView_EnlistHeader.FindControl("txtApplyStartDate"), WebForm_CommonControl_CalendarTextBox).Text
        e.NewValues("ApplyEndDate") = TryCast(FormView_EnlistHeader.FindControl("txtApplyEndDate"), WebForm_CommonControl_CalendarTextBox).Text
        e.NewValues("StartDate") = TryCast(FormView_EnlistHeader.FindControl("txtStartDate"), WebForm_CommonControl_CalendarTextBox).Text
        e.NewValues("EndDate") = TryCast(FormView_EnlistHeader.FindControl("txtEndDate"), WebForm_CommonControl_CalendarTextBox).Text
        e.NewValues("UpdateBy") = Page.User.Identity.Name
    End Sub


    Protected Sub SearchInternalUserControl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles SearchInternalUserControl1.SelectedIndexChanged
        TryCast(Me.FormView_EnlistHeader.FindControl("txtOwner"), TextBox).Text = Me.SearchInternalUserControl1.UserName
        TryCast(Me.FormView_EnlistHeader.FindControl("txtPhoneNumber"), TextBox).Text = Me.SearchInternalUserControl1.PhoneNumber
        TryCast(Me.FormView_EnlistHeader.FindControl("txtFaxNumber"), TextBox).Text = Me.SearchInternalUserControl1.FaxPhoneNumber
        TryCast(Me.FormView_EnlistHeader.FindControl("txtEmail"), TextBox).Text = Me.SearchInternalUserControl1.Email
    End Sub
End Class



Partial Class WebForm_ManageEnlistment_AddEnlistment

    Public Event Save_Click(ByVal source As Object, ByVal e As EventArgs)

    Public Sub InitData(ByVal intEnlistID As Integer)

        EnlistID = intEnlistID

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        '刪除EnlistTrainingCourse EnlistID =0的資料
        sql.AppendFormat("DELETE EnlistTrainingCourse ")
        sql.AppendFormat("WHERE EnlistID =0 AND CreateBy='{0}'; ", Page.User.Identity.Name)
        dat.AccData(sql)

        If intEnlistID = 0 Then
            '新增
            Me.DataAccessMode = FormViewMode.Insert
            Me.FormView_EnlistHeader.ChangeMode(FormViewMode.Insert)
            'Me.FormView_EnlistTrainingCourse.ChangeMode(FormViewMode.Insert)
        Else
            '編輯
            'Me.DataAccessMode = FormViewMode.Edit
            'Me.FormView_EnlistHeader.ChangeMode(FormViewMode.Edit)

            Me.FormView_EnlistHeader.ChangeMode(Me.DataAccessMode)
            'Me.FormView_EnlistTrainingCourse.ChangeMode(Me.DataAccessMode)

            ObjectDataSource1.SelectParameters.Item("EnlistID").DefaultValue = EnlistID

        End If

        ObjectDataSource2.SelectParameters.Item("EnlistID").DefaultValue = EnlistID
        GridView_EnlistTrainingCourse.DataBind()

        If Me.DataAccessMode = FormViewMode.ReadOnly Then
            GridView_EnlistTrainingCourse.Columns(GridView_EnlistTrainingCourse.Columns.Count - 1).Visible = False
            GridView_EnlistTrainingCourse.Columns(GridView_EnlistTrainingCourse.Columns.Count - 2).Visible = False
            GridView_EnlistTrainingCourse.Columns(GridView_EnlistTrainingCourse.Columns.Count - 3).Visible = False

            Me.btnSave.Visible = False
        Else
            Me.btnSave.Visible = True
        End If

        SqlDataSource1.SelectParameters.Item("EnlistID").DefaultValue = EnlistID
        GV.DataSourceID = SqlDataSource1.ID
        GV.DataBind()

    End Sub

#Region "Property"

    Public Property DataAccessMode() As FormViewMode
        Get
            Return ViewState("DataAccessMode")
        End Get
        Set(ByVal value As FormViewMode)
            ViewState("DataAccessMode") = value
        End Set
    End Property

    Public Property EnlistID() As Integer
        Get
            If ViewState("EnlistID") Is Nothing Then
                Return 0
            Else
                Return ViewState("EnlistID")

            End If

        End Get
        Set(ByVal value As Integer)
            ViewState("EnlistID") = value
        End Set
    End Property

    Public Property CourseID() As Integer
        Get
            If ViewState("CourseID") Is Nothing Then
                Return 0
            Else
                Return ViewState("CourseID")

            End If

        End Get
        Set(ByVal value As Integer)
            ViewState("CourseID") = value
        End Set
    End Property


#End Region


End Class